package cc.inik.act;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Map;
import java.util.Set;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button submit = findViewById(R.id.submit);
        Button read = findViewById(R.id.read);
        EditText username = findViewById(R.id.username);
        EditText pwd = findViewById(R.id.pwd);
        TextView tv = findViewById(R.id.res);
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences sh = getSharedPreferences("ink",MODE_PRIVATE);
                SharedPreferences.Editor  edit = sh.edit();
                String uname = username.getText().toString();
                String p = pwd.getText().toString();
                edit.putString("username",uname);
                edit.putString("pwd",p);
                boolean res = edit.commit();
                if(res){
                    Toast.makeText(MainActivity.this,"保存成功",Toast.LENGTH_SHORT);
                }else{
                    Toast.makeText(MainActivity.this,"保存失败",Toast.LENGTH_SHORT);
                }

            }
        });
        read.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences sh = getSharedPreferences("ink",MODE_PRIVATE);
                String u = sh.getString("username","无");
                String p = sh.getString("pwd","无");
                tv.setText("用户名"+u+"密码"+p);
            }
        });
    }

}